package com.project.${pkgname}.domain.service;

import com.project.common.domain.BizPage;
import com.project.common.domain.Convert;
import com.project.common.domain.Result;
import com.project.common.service.BaseService;
import com.project.${pkgname}.domain.entity.${className};
import com.project.${pkgname}.domain.repository.${className}Repository;
import com.project.${pkgname}.dto.${dtoName};
import com.project.${pkgname}.dto.${searchDtoName};
import javax.annotation.Resource;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: ${createName}
 * @blame: ${name} 应用服务
 * @date: ${time}
 **/
@Slf4j
@Service
public class ${className}Service extends BaseService {

    @Resource
    private ${className}Repository ${className?uncap_first}Repository;

    public Result save(${dtoName} ${dtoName?uncap_first}) {
        ${className} ${className?uncap_first}=null;
        if(null != ${dtoName?uncap_first}.getId()) {
            return ${className?uncap_first}Repository.findById(${dtoName?uncap_first}.getId()).map(item -> {
                Convert.mapSkipNull(${dtoName?uncap_first},item);
                item = ${className?uncap_first}Repository.save(item);
                if (item == null) {
                    return Result.error("更新失败");
                }
                return Result.success(item.convertTo(${dtoName}.class), "更新成功");
            }).orElseGet(() -> Result.error("更新失败"));
        }
        ${className?uncap_first} = ${dtoName?uncap_first}.convertTo(${className}.class);
        ${className?uncap_first} = ${className?uncap_first}Repository.save(${className?uncap_first});
        if (${className?uncap_first} == null) {
            return Result.error("保存失败");
        }
        return Result.success(${className?uncap_first}.convertTo(${dtoName}.class), "添加成功");
    }

    public Result findById(Long id) {
        ${className} ${className?uncap_first}=${className?uncap_first}Repository.findById(id).get();
        if(null != ${className?uncap_first}){
            return Result.success(${className?uncap_first}.convertTo(${dtoName}.class));
        }
        return Result.error("未查询到相关数据");
    }

    public BizPage<${dtoName}> findPage(${searchDtoName} searchDTO, Pageable pageable) {
        Specification<${className}> specification = (root, criteriaQuery, criteriaBuilder) -> {
            criteriaQuery.distinct(true);
            List<Predicate> predicates = new ArrayList<>();
            predicates.add(criteriaBuilder.equal(root.get("isDelete"), 0));
            //DOTD
            Predicate[] predicateArray = new Predicate[predicates.size()];
            return criteriaBuilder.and(predicates.toArray(predicateArray));
        };
        // Sort sort = Sort.by(Sort.Direction.DESC, "id");
        Page<${className}> list = ${className?uncap_first}Repository.findAll(specification, pageable);
        return BizPage.of(list, ${dtoName}.class);
    }

    /**
    * @param id
    * @param updateBy
    * @return
    */
    public Result delete(Long id, Long updateBy) {
        int m = ${className?uncap_first}Repository.delete(id, updateBy);
        if(m>0){
            return Result.success("删除成功！");
        }
        return Result.error("删除失败");
    }
}
